package com.nut.course.api.partner;

import com.nut.course.api.req.ChangePasswordReq;
import com.nut.course.api.req.LoginReq;
import com.nut.course.api.req.RegisterReq;
import com.nut.course.api.resp.UserAuth;
import com.nut.course.common.Holder;
import com.nut.course.common.Result;
import com.nut.course.entity.Partner;
import com.nut.course.interceptor.UserJwt;
import com.nut.course.service.PartnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 机构合作伙伴服务
 */
@RestController
@RequestMapping("/partner")
@Validated
@Slf4j
public class PartnerController {

    @Autowired
    private PartnerService partnerService;

    @PostMapping("register")
    public Result register(@RequestBody RegisterReq registerReq) {
        return partnerService.register(registerReq);
    }

    @PostMapping("login")
    public Result<UserAuth> login(@RequestBody LoginReq loginReq) {
        return partnerService.login(loginReq);
    }

    @PostMapping("changePassword")
    public Result changePassword(@RequestBody ChangePasswordReq changePasswordReq) {

        UserJwt userJwt = Holder.USER_JWT_THREAD_LOCAL.get();
        log.info("userId=" + userJwt.getUserId());
        changePasswordReq.setId(Integer.valueOf(userJwt.getUserId()));

        return partnerService.changePassword(changePasswordReq);
    }

    @GetMapping()
    public Result<Partner> getInfo() {

        UserJwt userJwt = Holder.USER_JWT_THREAD_LOCAL.get();
        log.info("userId=" + userJwt.getUserId());

        return partnerService.getById(Integer.valueOf(userJwt.getUserId()));
    }

    @PutMapping
    public Result update(@RequestBody Partner partner) {

        UserJwt userJwt = Holder.USER_JWT_THREAD_LOCAL.get();
        log.info("userId=" + userJwt.getUserId());
        partner.setId(Integer.valueOf(userJwt.getUserId()));

        return partnerService.updateById(partner);
    }

    @RequestMapping("checkToken")
    public Result<String> checkToken() {
        return Result.ok();
    }

    //TODO 注销登录 logout

}
